//
//  PointShopVC.swift
//  ShareUmbrella
//
//  Created by niupark on 2017/8/12.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import YSKit

class PointShopVC: YSBaseViewController {
    
    var tableView = UITableView(frame: .zero, style: .plain)
    let lblPoint = UILabel()
    var arrayData = NSMutableArray()
    
    override func setup() {
        super.setup()
    }
    /*
     *
     * 界面布局初始化
     */
    override func buildUI() {
        super.buildUI()

        self.automaticallyAdjustsScrollViewInsets = false;

        self.title = I18n("VC.LFSide.piontshop.titlte")
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalTo(self.view)
        }
        
        tableView.separatorStyle = .none
        
        tableView.tableHeaderView = createHeaderView()

        self.initData()
        
        tableView.register(GoodsTableViewCell.self, forCellReuseIdentifier: GoodsTableViewCell.className())
        
        tableView.reloadData()
    }
    
    //
    override func buildRx() {
        super.buildRx()
    }
    
    func initData() -> Void {
        let model1 = GoodsModel.init(userName: "Roger", goodsName: "小熊饼干150g", goodsImage: UIImage(named: "商品图片")!, goodsTime: "2017-07-14", goodsPoint: -233)
        arrayData.add(model1)
        
        let model2 = GoodsModel.init(userName: "Roger", goodsName: "小熊饼干150g", goodsImage: UIImage(named: "商品图片")!, goodsTime: "2017-07-14", goodsPoint: -233)
        arrayData.add(model2)
        
        let model3 = GoodsModel.init(userName: "Roger", goodsName: "小熊饼干150g", goodsImage: UIImage(named: "商品图片")!, goodsTime: "2017-07-14", goodsPoint: -233)
        arrayData.add(model3)
        
        let model4 = GoodsModel.init(userName: "Roger", goodsName: "小熊饼干150g", goodsImage: UIImage(named: "商品图片")!, goodsTime: "2017-07-14", goodsPoint: -233)
        arrayData.add(model4)
        
        let model5 = GoodsModel.init(userName: "Roger", goodsName: "小熊饼干150g", goodsImage: UIImage(named: "商品图片")!, goodsTime: "2017-07-14", goodsPoint: -233)
        arrayData.add(model5)
    }
    
    func createHeaderView() -> UIView{
        
        let vHeader = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 150)).ys.customize { (view) in
            view.backgroundColor = UIColor.init(colorLiteralRed: 80 / 255.0, green: 187 / 255.0, blue: 129 / 255.0, alpha: 1)
        }
        
        let vPointBg = UIView(frame: .zero).ys.customize { (view) in
            vHeader.addSubview(view)
            view.snp.makeConstraints({ (make) in
                make.centerX.equalTo(vHeader)
                make.height.width.equalTo(90)
                make.top.equalTo(vHeader).offset(15)
            })
            
        }
        
        let _ = UIImageView(image: UIImage(named: "积分圈")).ys.customize { (view) in
            vPointBg.addSubview(view)
            view.snp.makeConstraints({ (make) in
                make.edges.equalTo(vPointBg)
            })
        }
    
        self.lblPoint.ys.customize { (view) in
            vPointBg.addSubview(view)
            view.font = UIFont.boldSystemFont(ofSize: 25)
            view.textColor = UIColor.white
            view.text = "100"
            view.textAlignment = .center
            view.adjustsFontSizeToFitWidth = true
            view.numberOfLines = 2
            
            view.snp.makeConstraints({ (make) in
                make.edges.equalTo(vPointBg)
            })
        }
        
        
        let _ = UILabel(frame: .zero).ys.customize { (view) in
            vHeader.addSubview(view)
            view.font = UIFont.systemFont(ofSize: 15.0)
            view.textColor = UIColor.white
            view.text = "您的可用积分"
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(vPointBg.snp.bottom).offset(10)
                make.centerX.equalTo(vPointBg)
            })
        }
        return vHeader;

    }
    
}


extension PointShopVC: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: GoodsTableViewCell.className(), for: indexPath) as! GoodsTableViewCell
        cell.updateUI(goods: arrayData.object(at: indexPath.row) as! GoodsModel)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100.0;
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayData.count;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.ys.push(GoodDetailVC())
    }
}
